import Taro, { Component } from '@tarojs/taro'
import { View, Text, Checkbox } from '@tarojs/components'
import { AtButton,AtTag} from 'taro-ui'
import './Receipt.css'
import app from '../../../../main';

export default class Receipt extends Component{

    config={
        navigationBarTitleText: '快速咨询可接单时间'
    }

    constructor(){
        super(...arguments)
        this.state = {
            day:[
                {
                    week :'星期一',
                    shangwu : false,
                    zhongwu : false,
                    xiawu : false
                },
                {
                    week :'星期二',
                    shangwu : false,
                    zhongwu : false,
                    xiawu : false
                },
                {
                    week :'星期三',
                    shangwu : false,
                    zhongwu : false,
                    xiawu : false
                },
                {
                    week :'星期四',
                    shangwu : false,
                    zhongwu : false,
                    xiawu : false
                },
                {
                    week :'星期五',
                    shangwu : false,
                    zhongwu : false,
                    xiawu : false
                },
                {
                    week :'星期六',
                    shangwu : false,
                    zhongwu : false,
                    xiawu : false
                },
                {
                    week :'星期日',
                    shangwu : false,
                    zhongwu : false,
                    xiawu : false
                }
            ],
            resArr:[]
        }
    }

    changeshangwu(index){
        let me = this
        let day = this.state.day
        let arr = this.state.resArr
        console.log(index)
        if(day[index].shangwu == false){
            day[index].shangwu = true
            arr.push({
                time:'8:00-12:00',
                time_type:'上午',
                week:day[index].week
            })
            console.log(arr)
            me.setState({
                day:day,
                resArr:arr
            })
        }else{
            day[index].shangwu = false
            for(let i=0; i<arr.length; i++){
                if(arr[i].week == day[index].week){
                    if(arr[i].time == '8:00-12:00'){
                        arr.splice(i,1)
                    }
                }
            }
            console.log(arr)
        }
    }

    changezhongwu(index){
        let me = this
        let day = this.state.day
        let arr = this.state.resArr
        console.log(index)
        if(day[index].zhongwu == false){
            day[index].zhongwu = true
            arr.push({
                time:'12:00-18:00',
                time_type:'下午',
                week:day[index].week
            })
            console.log(arr)
            me.setState({
                day:day,
                resArr:arr
            })
        }else{
            day[index].zhongwu = false
            for(let i=0; i<arr.length; i++){
                if(arr[i].week == day[index].week){
                    if(arr[i].time == '12:00-18:00'){
                        arr.splice(i,1)
                    }
                }
            }
            console.log(arr)
        }
    }

    changexiawu(index){
        let me = this
        let day = this.state.day
        let arr = this.state.resArr
        console.log(index)
        if(day[index].xiawu == false){
            day[index].xiawu = true
            arr.push({
                time:'18:00-22:00',
                time_type:'晚上',
                week:day[index].week
            })
            console.log(arr)
            me.setState({
                day:day,
                resArr:arr
            })
        }else{
            day[index].xiawu = false
            for(let i=0; i<arr.length; i++){
                if(arr[i].week == day[index].week){
                    if(arr[i].time == '18:00-22:00'){
                        arr.splice(i,1)
                    }
                }
            }
            console.log(arr)
        }
    }


    get(){
        let me = this;
        let data = {
            
        }
        app.get('web/inquirySetting/quickInquirySettingData', data, function(res) {
            let arr = res.data.result.list
            let brr = me.state.day
            let crr = []
            console.log(arr)
           
            for(let val of arr){
                for(let gal of brr){
                    if(val.week == gal.week){
                        if(val.time_type == '上午'){
                            gal.shangwu = true
                        }
                        if(val.time_type == '下午'){
                            gal.zhongwu = true
                        }
                        if(val.time_type == '晚上'){
                            gal.xiawu = true
                        }
                    }
                }
            }

            for(let dal of arr){
                crr.push({
                    time:dal.time,
                    time_type:dal.time_type,
                    week:dal.week
                })
            }
            me.setState({
                resArr:crr,
                day:brr
            })
        })
      }

    componentWillMount(){
        this.get();;
    }

    go(){
        console.log(this.state.resArr)
        let data = {
            data:JSON.stringify(this.state.resArr)
        }
        app.post('web/inquirySetting/quickInquirySetting', data, function(res){
            console.log(res)
            app.redirectTo('../consultsetting/consultsetting')
        })
    }

    render(){
        return(
            <View className='Receipt'>
                {
                    this.state.day.map((item,index)=>{
                        return(
                            <View className='day'>
                                <View className='week'>{item.week}</View>
                                <View className='clean'></View>
                                <View className='time'>
                                    <Checkbox className='cbox' checked={item.shangwu == 0 ? false : true} onClick={this.changeshangwu.bind(this,index)}></Checkbox>
                                    <View className='text'>上午</View>
                                    <View className='num'>8:00-12:00</View>
                                </View>
                                <View className='time'>
                                    <Checkbox className='cbox' checked={item.zhongwu == 0 ? false : true} onClick={this.changezhongwu.bind(this,index)}></Checkbox>
                                    <View className='text'>下午</View>
                                    <View className='num'>12:00-18:00</View>
                                </View>
                                <View className='time'>
                                    <Checkbox className='cbox' checked={item.xiawu == 0 ? false : true} onClick={this.changexiawu.bind(this,index)}></Checkbox>
                                    <View className='text'>晚上</View>
                                    <View className='num'>18:00-22:00</View>
                                </View>                 
                            </View>
                        )
                    })
                } 
                <View style='width:750rpx;height:200rpx;'></View>
                <View className='btn'>
                    <AtButton onClick={this.go.bind(this)} type='primary'>保存</AtButton>
                </View>
            </View>
        )
    }
}
